#!/usr/bin/python

from __future__ import with_statement
from cStringIO import StringIO
#from contrib.sphinxapi import *
import sqlalchemy as sa
from Queue import Queue
from threading import Semaphore, Thread
import threading
from datetime import datetime, date
from time import strftime
import re, traceback, calendar, time, os, sys
from string import join

#from local
from lib.utils import sanitizestring, transcode3, transcode5
#from lib.database_qqshuqian import Database as shuqian_Database
#from lib.database_qqshuqian import tt_init, tt_close, tt_get
#from lib.database_qqshuqian import g_pagesize
from lib.database_qzone import Database as qzone_Database
from lib.database_qzone import get_engine

#debugmode = True
debugmode = False

workers_semaphore = Semaphore(8)
#workers_semaphore = Semaphore(1)

#following ids are for keyword 'java' and keyword 'hua4 zhuang1 pin3'
candidates = [512121895, 987586930, 397186112, 903977345, 164517527, 124978925, 281023391, 799347624, 870398211, 576869212, 331227562, 404347528, 22758397, 854908680, 923387083, 929207474, 384183315, 435850120, 327294658, 756770009, 942752049, 85387276, 312393835, 471597242, 565879793, 960611311, 603599102, 1025248867, 496077468, 289179029, 124955489, 285461099, 411585370, 756048772, 362146878, 328885639, 405063307, 100210, 37190625, 22790960,347314897, 463319496, 478227559, 297443649, 46911367, 646747149, 283900277, 543612719, 1156743516, 1143352703, 871749209, 112495455, 525378366, 396307896, 30787937, 290747256, 105127193, 412346496, 354403535, 313468769,5839524, 103831536, 327407245, 38300866, 578377780, 31315805, 751873335, 260303512, 277287067, 252315338, 61065080, 349711318, 726630658, 309522208, 56545673, 806943282, 287082984, 282010889, 34926028, 363216520,270552879, 358995998, 125175181, 549733593, 772632869, 923903732, 411728709, 403470025, 271547029, 10498139, 309207485, 765607920, 71913596, 370597204, 26464937, 180413256, 626088503, 236620414, 239818216, 409417405,576819320, 416741992, 113817893, 182430687, 541762523, 117542043, 164316776, 261781246, 120978472, 327271098, 350691557, 331257224, 452019782, 365874604, 346538961, 373433402, 617236210, 76479559, 477921768, 514752402,229491093, 501980893, 570713283, 630059870, 527496452, 452881799, 961802159, 573583803, 578151859, 632186663, 502212569, 851322930, 744401309, 253180235, 375284478, 89611518, 531203512, 18706669, 107159688, 869349420,467786002, 362595353, 122396836, 260479033, 675351136, 554686993, 529498115, 1040254240, 522230442, 962881241, 1026656958, 1107437524, 553228463, 138281985, 492633856, 461590263, 1032544553, 616722004, 443693992, 987280871,1154351423, 275243701, 541526353, 768876409, 395325867, 371224260, 775624741, 631843032, 377497922, 499283876, 497168478, 562127892, 286913646, 1061211815, 814662472, 410383719, 714775079, 1105490652, 352272652, 603904804,7315551, 458375550, 515609792, 31683381, 68271370, 419214800, 171077685, 63291216, 536472518, 1157720456, 277469626, 515074165, 853900903, 765739212, 632264582, 805242178, 354402085, 939900934, 731668702, 784676302,644312467, 928950535, 470246276, 784146992, 897018473, 460815482, 1099890550, 744513564, 987897279, 852654504, 343547413, 312015574, 1148446278, 471200098, 526623555, 735498482, 958011026, 25757301, 962016369, 352844334,634383374, 923207395, 295335942, 452042223, 355761547, 475538801, 568067518, 916737924, 272382452, 312630496, 535248989, 759905140, 344277075, 305832670, 115863033, 489343250, 1247839721, 626101373, 626268625, 1242021192,903179234, 120394419, 317418364, 814900232, 532374969, 394248397, 815865002, 752670419, 312664649, 87608526, 410935832, 894693957, 798299711, 602637552, 120512962, 923014849, 815613577, 987586930, 491397731, 1046549621]

def processQzone():
    dal = qzone_Database()
    engine2 = get_engine('crawlqzone', db_host='192.168.1.114', db_user='root', db_pass='')
    dal2 = qzone_Database(engine2)
    for uid in candidates:
        uid = str(uid)
        u = dal._queryOneUser(uid)
        [uid, uName, qzoneName, qzoneDescription, articleNum, done, modtime, seq, numclasses] = u
        dal2._commitUser(uid, uName, qzoneName, qzoneDescription, articleNum, done, modtime, numclasses)
        blogs = dal._queryUserBlog(uid)
        for b in blogs:
            [blog_id, title, time, num_comment, num_read, content, plaintext, cat_id, cat_name, seq] = b
            dal2._commitBlog(blog_id, uid, title, time, num_comment, num_read, content, plaintext, cat_id, cat_name)

processQzone()

